package
{
	import flash.geom.Point;
	
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.text.TextField;
	import starling.utils.deg2rad;

	/**
	 * 
	 * @author Polar
	 * @date 2012-12-30
	 * @function 
	 * 
	 */
	public class Game extends Sprite
	{
		private var customSprite:CustomSprite;
		private var mouseX:Number=0;
		private var mouseY:Number=0;
		
		private var lastDistance:int=0;
		public function Game()
		{
			addEventListener(Event.ADDED_TO_STAGE,onAdded);
		}
		private function onAdded(e:Event):void
		{
			customSprite = new CustomSprite(200,200);
			customSprite.x = stage.stageWidth>>1;
			customSprite.y = stage.stageHeight>>1;
			addChild(customSprite);
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
			
			
			stage.addEventListener(TouchEvent.TOUCH,onTouch);
			customSprite.addEventListener(TouchEvent.TOUCH,onTouchSprite);
		}
		private function onEnterFrame(e:Event):void
		{
			customSprite.update();	
//			customSprite.x = mouseX;
//			customSprite.y = mouseY;
			
//			customSprite.x -= (customSprite.x - mouseX)*0.1;
//			customSprite.y -= (customSprite.y - mouseY)*0.1;
		}
		private function onTouch(e:TouchEvent):void
		{
			var touch:Touch = e.getTouch(stage);
			var pos:Point = touch.getLocation(stage);
			
			
			mouseX = pos.x;
			mouseY = pos.y;
		}
		private function onTouchSprite(e:TouchEvent):void
		{
			var touchs:Vector.<Touch> = e.touches;
//			var clicked:DisplayObject = e.currentTarget as DisplayObject;
			if(touchs.length == 2)
			{
				var finger1:Touch = touchs[0];
				var finger2:Touch = touchs[1];
				var distance:int;
				var dx:int;
				var dy:int;
				if(finger1.phase == TouchPhase.MOVED && finger2.phase== TouchPhase.MOVED)
				{
					dx = Math.abs(finger1.globalX - finger2.globalX);
					dy = Math.abs(finger1.globalY - finger2.globalY);
					distance = Math.sqrt(dx*dx+dy*dy);
					if(lastDistance != 0)
					{
						customSprite.scaleX *= Number(distance)/Number(lastDistance);						
						customSprite.scaleY *= Number(distance)/Number(lastDistance);
					}
					lastDistance = distance;
				}
			}

		}

	}
}